package com.tongann.wind.service.impl;


import com.tongann.wind.mapper.OrganizationMapper;
import com.tongann.wind.mapper.UserMapper;
import com.tongann.wind.model.Organization;
import com.tongann.wind.model.User;
import com.tongann.wind.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    OrganizationMapper organizationMapper;


    @Override
    public List<User> getAllUser() {
        return userMapper.getUserAll();
    }

    @Override
    public void addNewUser(User user) {
        String org = user.getOrganization().getName();
        Organization organization = organizationMapper.selectOrgByName(org);
        if (organization == null)
        {
            organization = new Organization();
            String uuid = UUID.randomUUID().toString().replace("-", "");
            organization.setUuid(uuid);
            organization.setName(org);
            organization.setCreateTime(new Date());
            organizationMapper.insertOrg(organization);
        }
        else
        {
            user.setOrgId(organization.getUuid());
        }
        userMapper.insertUser(user);
    }

    @Override
    public User getUserByUsername(String username) {
        return userMapper.getUserByUsername(username);
    }
}
